/*
 * @Author: Mister Wang
 * @Date: 2021-01-06 11:46:55
 * @LastEditTime: 2021-03-16 14:05:36
 * @LastEditors: Please set LastEditors
 * @Description: 通用函数
 * @FilePath: \sende_nuxt\utils\Utils.js
 */

/**
 *@param: 
 *@description: 递归实现树形目录(二)
 *@author: Mister Wang
 *@date: 2021-01-06 11:47:04
*/
export const sonsTree = (obj, arr) => {
    var children = new Array();
    for (var i = 0; i < arr.length; i++) {
        // 如果当前元素的id等于arr[i]的parent_id，说明是该当前元素的子元素
        if (arr[i].parent_id == obj.id) {
            //递归出子元素, (查看子元素是否还存在子元素)
            sonsTree(arr[i], arr);
            children.push(arr[i]);
        }
    }
    // 存在子元素，将子元素赋值到当前元素的children上。
    if (children.length > 0) {
        obj.children = sortObj(children, 'position');
    }
    return obj;
}
/**
 *@param: 
 *@description: 递归实现树形目录(一)
 *@author: Mister Wang
 *@date: 2021-01-06 15:32:32
*/
export const treeUtils = (data) => {
    let ptree = [];
    let tree = [];
    for (let i = 0; i < data.length; i++) {
        //获取父元素, 排除一级路由，存在子目录
        if (data[i].children !== '' && data[i].level > 1) {
            let o = sonsTree(data[i], data);
            ptree.push(o);
        }
    }
    // console.info(ptree);
    return ptree;
}
/**
 *@param: arr >> 需要进行排序的数组; attr >> 属性
 *@description: 对数组进行排序(插入排序)
 *@author: Mister Wang
 *@date: 2021-01-06 15:33:33
*/
export const sortObj = (arr, attr) => {
    let s = '';
    //升序
    for (let i = 1; i < arr.length; i++) {
        for (let j = i; j > 0; j--) {
            let condition = false;
            if(attr) {
                condition = arr[j][attr] < arr[j - 1][attr];
            } else {
                condition = arr[j] < arr[j - 1];
            }
            if (condition) {
                s = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = s;
                // console.log(arr); // 可以打印出来每一个改变的步骤 　　　　
            }
        }
    }
    //降序
    for (let i = 1; i < arr.length; i++) {
        for (let j = i; j > 0; j--) {
            let condition = false;
            if(attr) {
                condition = arr[j][attr] < arr[j - 1][attr];
            } else {
                condition = arr[j] < arr[j - 1];
            }
            if (condition) {
                s = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = s;
                // console.log(arr); // 可以打印出来每一个改变的步骤 　　　　
            }
        }
    }
    return arr;
}

/**
 *@param: url，路由地址
 *@description: 截取url路径
 *@author: Mister Wang
 *@date: 2021-01-13 13:56:10
*/
export const spliteParams = (url) => {
    let arr = [];
    // 判断是否存在子级
    let path = url.split('/');
    // 删除第一个空字符串
    path.shift();
    // 判断是否存在子级
    path.map(item => {
        if(/\?{1}/i.test(item)) {
            let url = item.slice(0, item.indexOf('?'))
            arr.push(url);
        } else {
            arr.push(item);
        }
    })
    return arr 
}

/**
 *@param: 参数
*@description:
*@author: Mister Wang
*@date: 2020-12-23 22:39:00
*/
export const getElementY = elem => {
    return window.pageYOffset + document.querySelector(elem).getBoundingClientRect().top
}
/**
 * @description: 滚动到指定元素 方法
 * @param {String} elem DOM元素
 * @param {Number} duration 滚动动画执行的时间
 * @param {Number} offset 滚动偏移量
 */
export const scrollToElem = (elem, duration = 1000, offset = 0) => {
// 初始位置
const startingY = window.pageYOffset
const elementY = getElementY(elem)
// 需要去滚动的距离
const diff = elementY - startingY + offset
// 如果 diff = 0
if (!diff) return
const easing = t => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
let start
window.requestAnimationFrame(function step (timestamp) {
    if (!start) start = timestamp
    // 计算时间的差值，根据差值计算偏移量
    const time = timestamp - start
    let percent = Math.min(time / duration, 1)
    percent = easing(percent)
    window.scrollTo(0, startingY + diff * percent)

    if (time < duration) {
    window.requestAnimationFrame(step)
    }
})
}
/**
 *@param: 
 *@description: 匹配指定字符串 返回所有该字符串的索引（Array）
 *@author: Mister Wang
 *@date: 2021-03-03 11:50:26
*/
export const findAll = (a, x) => {
  var results = [],
      len = a.length,
      pos = 0;
  while(pos < len) {
    pos = a.indexOf(x, pos);
    if (pos === -1) {//未找到就退出循环完成搜索
      break;
    }
    results.push(pos);//找到就存储索引
    pos += 1;//并从下个位置开始搜索
  }
  return results;
}

/**
 *@param: 
 *@description: 对属性进行排序
 *@author: Mister Wang
 *@date: 2021-03-05 11:33:14
*/
export const compare = (property) => {
    return function (a, b) {
        let value1 = a[property];
        let value2 = b[property];
        return value1 - value2;
    }
}
